package com.mgvlife.ddlw.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.mgvlife.ddlw.entity.SuperAdmin;
import com.mgvlife.ddlw.formbean.SuperAdminForm;
import com.mgvlife.ddlw.service.SuperAdminService;
import com.mgvlife.ddlw.util.HttpSessionUtil;
import com.mgvlife.ddlw.vo.Result;
import com.mgvlife.ddlw.vo.VoSuperAdmin;
import com.xinhuo.dao.DaoSupport;
import com.xinhuo.util.QueryResult;
import com.xinhuo.vo.JsonEasyUI;

@Service("superAdminService")
public class SuperAdminServiceImpl extends DaoSupport implements SuperAdminService {

	public Result superAdminLogin(SuperAdminForm dataForm) throws Exception {
		String name = dataForm.getName();
		String password = dataForm.getPassword();
		String whereSQL = "name=? and password=?";
		Object[] whereParams = {name, password};
		SuperAdmin superAdmin = this.find(SuperAdmin.class, whereSQL, whereParams);
		if (superAdmin == null) {
			return Result.passwordError();
		}
		HttpSessionUtil.putSuperAdmin(superAdmin.getId(), superAdmin.getName());
		return new Result("success", "登陆成功");
	}

	public Result superAdminUpdate(SuperAdminForm dataForm) throws Exception {
		if (!dataForm.getPassword2().equals(dataForm.getPwdRepeat())) {
			return new Result("error", "新输入密码不一致");
		}
		String superAdminId = dataForm.getId();
		SuperAdmin superAdmin = null;
		if (superAdminId == null || superAdminId.trim().equals("")) {
			return new Result("error", "无此管理员");
		} else {
			superAdmin = this.findById(SuperAdmin.class, superAdminId);
		}
		if (!superAdmin.getPassword().equals(dataForm.getPassword())) {
			return new Result("error", "原密码不正确");
		}
		superAdmin.setPassword(dataForm.getPwdRepeat());
		this.update(superAdmin);
		return Result.success();
	}

	public JsonEasyUI<VoSuperAdmin> getData(SuperAdminForm dataForm) {
		JsonEasyUI<VoSuperAdmin> result = new JsonEasyUI<VoSuperAdmin>();
		QueryResult<SuperAdmin> qr = this.getScrollData(SuperAdmin.class, null, null,
				dataForm.getSearchTitle(), dataForm.getSearch(),
				dataForm.getPage(), dataForm.getRows(), dataForm.getSort(),
				dataForm.getOrder());
		result.setTotal(qr.getTotalrecord());
		List<VoSuperAdmin> admin_l = new ArrayList<VoSuperAdmin>();
		for (SuperAdmin superAdmin : qr.getResultList()) {
			admin_l.add(new VoSuperAdmin(superAdmin));
		}
		result.setRows(admin_l);
		return result;
	}

}
